#include "main_window.h"
#include "ui_main_window.h"
#include <QDebug>
#include <QRect>
#include <QToolButton>
#include <QColor>
#include <QMovie>
#include <QTimer>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QtWidgets/QWidget>
#include <QTextCodec>
#include <QString>
#include <QUrl>
#include <QThread>
#include <QMediaPlayer>
Main_Window::Main_Window(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Main_Window)
{

    ui->setupUi(this);
    this->Beep_page = new Beep_window;//实例化led窗口
    this->Music_page = new Music_window;
    this->Pichoose_page = new Pichoose_window;
    this->Map_page = new Map_window;
    ui->change->installEventFilter(this);
    time_weather_data my_data(this);
    weather_init(city_id);
    my_data.get_time(&(this->year),&(this->month),&(this->day),&(this->hour),&(this->minute),&(this->second),&(this->time_flag));
    timer_obj = new QTimer(this);
    connect(this->timer_obj,&QTimer::timeout,[=](){
       this->second++;
        if(old_picture_flag != picture_flag){
            QPixmap pbuty;
            //QString str = QString::number();
            picture_name.append(Pichoose_page->PicturePath);
            picture_name.append(Pichoose_page->fileList.at(picture_flag));
            pbuty.load(picture_name);
            ui->gif_label->setPixmap(pbuty);
            ui->gif_label->setScaledContents(true);
            picture_name = "\0";
            old_picture_flag = picture_flag;
        }
        if(this->second >= 60){
            this->second = 0;
            this->minute++;
        //    weather_init();
            if(this->minute >=60){

                this->minute = 0;
                this->hour++;
                if(this->hour>=24){
                    this->hour = 0;
                    this->day++;
                    weather_init(city_id);
                    //接下来做月份，闰年判断，不想写了
                }
            }
            update();
        }
    });
    this->show_mainwindow();
    timer_obj->start(1000);
}

void Main_Window::show_mainwindow(){

    QPalette pe;
    pe.setColor(QPalette::WindowText,Qt::white);

    QPixmap pic(":/picture/weather_window/切换.png");
    ui->change->setPixmap(pic);
    ui->change->setScaledContents(true);
    /*主菜单栏的右侧*/
    QPalette pex;
    pex.setColor(QPalette::ButtonText,Qt::white);   //文字颜色
    pex.setColor(QPalette::Button ,QColor(0,64,128));       //背景色

    ui->Beep_but->setStyleSheet("QToolButton{background-color: rgb(0,64,128);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}"
                                            "QToolButton:hover{background-color:rgb(0,64,128); color: white;}"
                                            "QToolButton:pressed{background-color:rgb(0,54,108);border-style: inset;}");

    ui->Crema_but->setStyleSheet("QToolButton{background-color: rgb(0,64,128);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}"
                                 "QToolButton:hover{background-color:rgb(0,64,128); color: white;}"
                                 "QToolButton:pressed{background-color:rgb(0,54,108);border-style: inset;}");
    ui->Picture_but->setStyleSheet("QToolButton{background-color: rgb(0,64,128);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}"
                                   "QToolButton:hover{background-color:rgb(0,64,128); color: white;}"
                                   "QToolButton:pressed{background-color:rgb(0,54,108);border-style: inset;}");
    ui->Music_but->setStyleSheet("QToolButton{background-color: rgb(0,64,128);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}"
                                 "QToolButton:hover{background-color:rgb(0,64,128); color: white;}"
                                 "QToolButton:pressed{background-color:rgb(0,54,108);border-style: inset;}");


    ui->Beep_but->setPalette(pex);
    ui->Crema_but->setPalette(pex);
    ui->Picture_but->setPalette(pex);
    ui->Music_but->setPalette(pex);

    ui->Beep_but->setIcon(QIcon(":/picture/main_window/喇叭.png"));
    ui->Beep_but->setIconSize(QSize(201,105));
    ui->Beep_but->setText(QString::fromLocal8Bit("喇叭"));
    ui->Beep_but->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    ui->Crema_but->setIcon(QIcon(":/picture/main_window/天气.png"));
    ui->Crema_but->setIconSize(QSize(201,105));
    ui->Crema_but->setText(QString::fromLocal8Bit("天气"));
    ui->Crema_but->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    ui->Picture_but->setIcon(QIcon(":/picture/main_window/picture.png"));
    ui->Picture_but->setIconSize(QSize(201,105));
    ui->Picture_but->setText(QString::fromLocal8Bit("照片"));
    ui->Picture_but->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    ui->Music_but->setIcon(QIcon(":/picture/main_window/music.png"));
    ui->Music_but->setIconSize(QSize(201,105));
    ui->Music_but->setText(QString::fromLocal8Bit("音乐"));
    ui->Music_but->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    /*桌面照片*/
    QPixmap pbuty;
    QString str = QString::number(picture_flag);
    picture_name.append(Pichoose_page->PicturePath);
    picture_name.append(str);
    picture_name.append(".jpg");
    pbuty.load(picture_name);
    ui->gif_label->setPixmap(pbuty);
    ui->gif_label->setScaledContents(true);
    picture_name = "\0";
}
Main_Window::~Main_Window()
{
    delete ui;
}
void Main_Window::paintEvent(QPaintEvent *event){
    QPainter my_painter(this);
    QPen pen(QColor(255,255,255));
    QFont font;
    font.setFamily("Microsoft YaHei");

    my_painter.setPen(pen);

    /*背景颜色*/
    my_painter.setBrush(QBrush(QColor(234,254,254)));
    my_painter.drawRect(this->rect());
    /*第一个框*/
    my_painter.setBrush(QBrush(QColor(0,33,100)));
    QRectF first_rect(9,9,330,151);
    my_painter.drawRoundedRect(first_rect, 10, 10);
    //时分妙
    font.setPointSize(60);
    my_painter.setFont(font);
    char strNum[5];
    sprintf(strNum,"%02d",this->hour);//整形到字符串的转换，并且自动补0
    my_painter.drawText(QRect(14,5,150,100),strNum);

    my_painter.drawText(QRect(105,0,20,100),":");
    sprintf(strNum,"%02d",this->minute);
    my_painter.drawText(QRect(130,5,150,100),strNum);
    font.setPointSize(30);
    my_painter.setFont(font);

    if(this->time_flag == 0) my_painter.drawText(QRect(235,50,100,100),"AM");
    else if(this->time_flag == 1) my_painter.drawText(QRect(235,50,100,100),"PM");
    //分界线
    my_painter.drawLine(QPoint(9,120),QPoint(338,120));
    //年月日
    font.setPointSize(16);
    my_painter.setFont(font);

    sprintf(strNum,"%04d",this->year);
    my_painter.drawText(QRect(16,125,100,100),strNum);
    my_painter.drawText(QRect(70,125,100,100),"年");
    sprintf(strNum,"%02d",this->month);
    my_painter.drawText(QRect(100,125,100,100),strNum);
    my_painter.drawText(QRect(130,125,100,100),"月");
    sprintf(strNum,"%02d",this->day);
    for(int i=1;i<=31;i++){
        if(i==this->day) my_painter.drawText(QRect(160,125,100,100),strNum);
    }
    my_painter.drawText(QRect(190,125,100,100),"日");
    /*第二个框*/
    QRectF second_rect(9,166,330,151);
    my_painter.drawRoundedRect(second_rect, 10, 10);
    //天气
    font.setPointSize(13);
    my_painter.setFont(font);

  //  qputenv("QT_IM_MODULE", QByteArray("xyinput"));
    switch (this->weather) {
        case 0:{my_painter.drawPixmap(200,180,100,100,QPixmap(":/picture/main_window/weather/晴天.png"));
                my_painter.drawText(QRect(230,285,150,50),"晴天");
                }break;
        case 1:{my_painter.drawPixmap(200,180,100,100,QPixmap(":/picture/main_window/weather/雨天.png"));
            my_painter.drawText(QRect(230,285,150,50),"雨天");
            }break;
        case 2:{my_painter.drawPixmap(200,180,100,100,QPixmap(":/picture/main_window/weather/多云.png"));
            my_painter.drawText(QRect(230,285,150,50),"多云");
            }break;
        case 3:{my_painter.drawPixmap(200,180,100,100,QPixmap(":/picture/main_window/weather/阵雪.png"));
            my_painter.drawText(QRect(230,285,150,50),"雪天");
            }break;
        case 4:{my_painter.drawPixmap(200,180,100,100,QPixmap(":/picture/main_window/weather/阴天.png"));
        my_painter.drawText(QRect(230,285,150,50),"阴天");
        }break;
    }
    //地点
    font.setPointSize(25);
    my_painter.setFont(font);
    my_painter.drawText(QRect(14,166,150,50),Map_page->city_name);
    //温度
    font.setPointSize(45);
    my_painter.setFont(font);
    sprintf(strNum,"%02d",this->tempature);
    my_painter.drawText(QRect(14,210,150,80),strNum);
    my_painter.drawText(QRect(90,210,150,80),"°C");
    //第三个框
    QRectF third_rect(9,323,330,151);
    my_painter.drawRoundedRect(third_rect, 10, 10);
    font.setPointSize(13);
    my_painter.setFont(font);
    if(this->wifi_state == 1){
        my_painter.drawPixmap(50,340,100,100,QPixmap(":/picture/main_window/wifi.png"));
        my_painter.drawText(QRect(70,440,150,80),"已连接");
    }
    else{
        my_painter.drawPixmap(50,340,100,100,QPixmap(":/picture/main_window/没wifi.png"));
        my_painter.drawText(QRect(70,440,150,80),"未连接");
    }
    if(this->lanya_state == 0){
        my_painter.drawPixmap(200,340,100,100,QPixmap(":/picture/main_window/蓝牙-关.png"));
        my_painter.drawText(QRect(220,440,150,80),"未连接");
    }
    else{
        my_painter.drawPixmap(200,340,100,100,QPixmap(":/picture/main_window/蓝牙.png"));
        my_painter.drawText(QRect(220,440,150,80),"已连接");
    }

}


void Main_Window::on_Beep_but_clicked()
{
    this->hide();
    this->Beep_page->show();
    this->GPIO_File->open(QIODevice::WriteOnly);
    this->GPIO_File->write("19",2);
    this->GPIO_File->close();
}

void Main_Window::on_Crema_but_clicked()
{
    this->hide();
    this->Map_page->show();
}
void Main_Window::on_Music_but_clicked()
{
    this->hide();
    this->Music_page->show();
}
void Main_Window::weather_init(QString id){
    QString webaddr="http://wthrcdn.etouch.cn/weather_mini?citykey=";
    webaddr.append(id);
    QNetworkRequest network_request;
    network_request.setUrl(QUrl(webaddr));
    connect(manage,SIGNAL(finished(QNetworkReply *)),this,SLOT(replyfinished(QNetworkReply*)));
    manage->get(network_request);
}
void Main_Window::replyfinished(QNetworkReply *reply)
{
    QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

      if(reply->error() == QNetworkReply::NoError)
      {
          QString all = reply->readAll();
         // qDebug()<<all;
          weather_dispose(all);
      }
      reply->deleteLater();
}
void Main_Window::weather_dispose(QString str){
    QByteArray byte_array;
        QJsonParseError json_error;
        QJsonDocument document = QJsonDocument::fromJson(byte_array.append(str),&json_error);
        if(json_error.error == QJsonParseError::NoError)
        {
             QJsonObject obj = document.object();
             QJsonValue value  = obj.take("desc");//qjsonvalue生成json文档中对象的值的对象
             if(value.toString() != "OK")
             {
                 QMessageBox::warning(this,QString::fromLocal8Bit("抱歉"),QString::fromLocal8Bit("暂无此城市信息"),QMessageBox::Ok,QMessageBox::Ok);
                 QMessageBox::warning(this,"抱歉","暂无此城市的天气情况",QMessageBox::Ok,QMessageBox::Ok);
                 return;
             }
             QJsonValue da = obj.take("data");//获取data数据对象里面的值

             QString wendu = da.toObject().take("low").toString();
             this->tempature = wendu.toInt();

             QJsonValue da1 = da.toObject().take("forecast");
             QJsonArray array = da1.toArray();

             qDebug()<<array;
             QJsonValue value1 = array.at(0);
             QJsonObject obj1 = value1.toObject();
             QString type1 = obj1.take("type").toString();
             QString low1 = obj1.take("low").toString();
             QString high1 = obj1.take("high").toString();
             QString date1 = obj1.take("date").toString();

             this->Map_page->day1_l = low1.mid(3);
            this->Map_page->temp_now = low1.mid(3);
             QStringList list = low1.mid(3).split("℃");
            this->tempature = list.at(0).toInt();
            qDebug()<<list.at(0);
             this->Map_page->day1_h = high1.mid(3);
             this->Map_page->day1_time = date1;

       //      qDebug()<<low1<< this->Map_page->day1_l<<high1<<high1.toInt();
             if(type1 == "晴"){
                 this->Map_page->day1_tq = 0;
                 this->Map_page->tq_flag = 0;
             }
             else if(type1 == "大雨" || type1 == "小雨"){
                 this->Map_page->day1_tq = 1;
                 this->Map_page->tq_flag = 1;
             }
             else if(type1 == "多云"){
                 this->Map_page->day1_tq = 2;
                         this->Map_page->tq_flag = 2;
             }
             else if(type1 == "小雪" ||type1 == "大雪"){
                 this->Map_page->day1_tq = 3;
                 this->Map_page->tq_flag = 3;
             }
             else if(type1 == "阴"){
                 this->Map_page->day1_tq = 4;
                         this->Map_page->tq_flag = 4;
             }
             QJsonValue value2 = array.at(1);
             QJsonObject obj2 = value2.toObject();
             QString type2 = obj2.take("type").toString();
             QString low2 = obj2.take("low").toString();
             QString high2 = obj2.take("high").toString();
             QString date2 = obj2.take("date").toString();

             this->Map_page->day2_l = low2.mid(3);

           // this->tempature = this->Map_page->temp_now;
             this->Map_page->day2_h = high2.mid(3);
             this->Map_page->day2_time = date2;
             if(type2 == "晴"){
                 this->Map_page->day2_tq = 0;
             }
             else if(type2 == "小雨"||type2 == "大雨"){
                 this->Map_page->day2_tq = 1;
             }
             else if(type2 == "多云"){
                 this->Map_page->day2_tq = 2;
             }
             else if(type2 == "小雪"||type2 == "大雪"){
                 this->Map_page->day2_tq = 3;
             }
            else if(type2 == "阴"){
                this->Map_page->day2_tq =4;
             }
             QJsonValue value3 = array.at(2);
             QJsonObject obj3 = value3.toObject();
             QString type3 = obj3.take("type").toString();
             QString low3 = obj3.take("low").toString();
             QString high3 = obj3.take("high").toString();
             QString date3 = obj3.take("date").toString();

             this->Map_page->day3_l = low3.mid(3);

           // this->tempature = this->Map_page->temp_now;
             this->Map_page->day3_h = high3.mid(3);
             this->Map_page->day3_time = date3;
             if(type3 == "晴"){
                 this->Map_page->day3_tq = 0;
             }
             else if(type3 == "小雨"||type3 == "大雨"){
                 this->Map_page->day3_tq = 1;
             }
             else if(type3 == "多云"){
                 this->Map_page->day3_tq = 2;
             }
             else if(type3 == "小雪"||type3=="大雪"){
                 this->Map_page->day3_tq = 3;
             }
             else if(type3 == "阴"){
                 this->Map_page->day3_tq =4;
              }

             QJsonValue value4 = array.at(3);
             QJsonObject obj4 = value4.toObject();
             QString type4 = obj4.take("type").toString();
             QString low4 = obj4.take("low").toString();
             QString high4 = obj4.take("high").toString();
             QString date4 = obj4.take("date").toString();

             this->Map_page->day4_l = low4.mid(3);

           // this->tempature = this->Map_page->temp_now;
             this->Map_page->day4_h = high4.mid(3);
             this->Map_page->day4_time = date4;
             if(type4 == "晴"){
                 this->Map_page->day4_tq = 0;
             }
             else if(type4 == "小雨"||type4 =="大雨"){
                 this->Map_page->day4_tq = 1;
             }
             else if(type4 == "多云"){
                 this->Map_page->day4_tq = 2;
             }
             else if(type4 == "小雪"||type4 =="大雪"){
                 this->Map_page->day4_tq = 3;
             }
             else if(type4 == "阴"){
                 this->Map_page->day4_tq =4;
              }

             QJsonValue value5 = array.at(4);
             QJsonObject obj5 = value5.toObject();
             QString type5 = obj5.take("type").toString();
             QString low5 = obj5.take("low").toString();
             QString high5 = obj5.take("high").toString();
             QString date5 = obj5.take("date").toString();

             this->Map_page->day5_l = low5.mid(3);

           // this->tempature = this->Map_page->temp_now;
             this->Map_page->day5_h = high5.mid(3);
             this->Map_page->day5_time = date5;
             if(type5 == "晴"){
                 this->Map_page->day5_tq = 0;
             }
             else if(type5 == "小雨"||type5 == "大雨"){
                 this->Map_page->day5_tq = 1;
             }
             else if(type5 == "多云"){
                 this->Map_page->day5_tq = 2;
             }
             else if(type5 == "小雪"||type5 == "大雪"){
                 this->Map_page->day5_tq = 3;
             }
             else if(type5 == "阴"){
                 this->Map_page->day5_tq =4;
              }

             if(type1 == "晴"){
                 this->weather = 0;
             }
             else if(type1 == "小雨"|| type1 =="大雨"){
                 this->weather = 1;
             }
             else if(type1 == "多云"){
                 this->weather = 2;
             }
             else if(type1 == "小雪"||type1 == "大雪"){
                 this->weather = 3;
             }
             else if(type1 =="阴"){
                 this->weather = 4;
             }
      }
    update();
}

bool Main_Window::eventFilter(QObject *obj, QEvent *event)
{
    QPixmap pic;

    if (obj == ui->change)//指定某个QLabel
     {
         if (event->type() == QEvent::MouseButtonPress) //鼠标点击
         {
             QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); // 事件转换
             if(mouseEvent->button() == Qt::LeftButton)
             {
                 if(this->Map_page->city_flag == 0){
                     this->Map_page->city_name = "厦门";
                     city_id = "101230201";
                     this->Map_page->city_flag=1;

                 }
                 else if(this->Map_page->city_flag == 1){
                     this->Map_page->city_name = "泉州";
                     this->Map_page->city_flag=2;
                     city_id = "101230501";
                 }
                 else if(this->Map_page->city_flag == 2){
                     this->Map_page->city_name = "兰州";
                     this->Map_page->city_flag=0;
                     city_id = "101160101";
                 }
                 weather_init(city_id);
                 return true;
             }
             else
             {
                 return false;
             }
         }
         else
         {
             return false;
         }
     }
}
void Main_Window::on_Picture_but_clicked()
{
    this->hide();
    Pichoose_page->show();
}
